using System;
using System.Net.NetworkInformation;

namespace IQIGame.Onigao.GamePlay
{
    public static class DeviceInfoUtil
    {
        public static string GetMacAddress()
        {
            string physicalAddress = "";

            NetworkInterface[] nice = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface adaper in nice)
            {

                if (adaper.Description == "en0")
                {
                    physicalAddress = adaper.GetPhysicalAddress().ToString();
                    break;
                }
                else
                {
                    physicalAddress = adaper.GetPhysicalAddress().ToString();

                    if (physicalAddress != "")
                    {
                        break;
                    };
                }
            }

            return physicalAddress;
        }

        public static string GetMacAddressFormat()
        {
            var macAddress = GetMacAddress();
            if("".Equals(macAddress))
            {
                return "Unknown";
            }
            if (macAddress.Length != 12)
            {
                throw new ArgumentException("Invalid MAC Address length mac = " + macAddress);
            }

            // 每两个字符后面加一个分隔符，除了最后两个字符
            System.Text.StringBuilder formatted = new System.Text.StringBuilder();
            for (int i = 0; i < macAddress.Length; i += 2)
            {
                if (i > 0)
                    formatted.Append("-"); // 在每一对字符前添加分隔符，但不包括字符串的开始位置

                formatted.Append(macAddress[i]);
                formatted.Append(macAddress[i + 1]);
            }

            return formatted.ToString();
        }
    }
}
